class MyPromise {
    constructor(executor) {
      this.status = 'pending'; // 初始状态为pending
      this.value = undefined; // resolve时传递的值
      this.reason = undefined; // reject时传递的原因
      this.onResolvedCallbacks = []; // resolve时的回调函数队列
      this.onRejectedCallbacks = []; // reject时的回调函数队列
  
      const resolve = (value) => {
        if (this.status === 'pending') {
          this.status = 'fulfilled';
          this.value = value;
          this.onResolvedCallbacks.forEach(callback => callback(this.value));
        }
      };
  
      const reject = (reason) => {
        if (this.status === 'pending') {
          this.status = 'rejected';
          this.reason = reason;
          this.onRejectedCallbacks.forEach(callback => callback(this.reason));
        }
      };
  
      try {
        executor(resolve, reject);
      } catch (error) {
        reject(error);
      }
    }
  
    then(onFulfilled, onRejected) {
      onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : (value) => value;
      onRejected = typeof onRejected === 'function' ? onRejected : (reason) => { throw reason; };
  
      let promise2;
  
      if (this.status === 'pending') {
        // 如果当前状态为pending，则将回调函数加入对应的队列
        promise2 = new MyPromise((resolve, reject) => {
          this.onResolvedCallbacks.push(() => {
            try {
              const x = onFulfilled(this.value);
              resolvePromise(promise2, x, resolve, reject);
            } catch (error) {
              reject(error);
            }
          });
  
          this.onRejectedCallbacks.push(() => {
            try {
              const x = onRejected(this.reason);
              resolvePromise(promise2, x, resolve, reject);
            } catch (error) {
              reject(error);
            }
          });
        });
      } else if (this.status === 'fulfilled') {
        // 如果当前状态为fulfilled，则立即执行onFulfilled，并返回一个新的promise
        setTimeout(() => {
          try {
            const x = onFulfilled(this.value);
            resolvePromise(promise2, x, resolve, reject);
          } catch (error) {
            reject(error);
          }
        }, 0);
      } else if (this.status === 'rejected') {
        // 如果当前状态为rejected，则立即执行onRejected，并返回一个新的promise
        setTimeout(() => {
          try {
            const x = onRejected(this.reason);
            resolvePromise(promise2, x, resolve, reject);
          } catch (error) {
            reject(error);
          }
        }, 0);
      }
  
      return promise2;
    }
  
    static resolve(value) {
      return new MyPromise(resolve => resolve(value));
    }
  
    static reject(reason) {
      return new MyPromise((_, reject) => reject(reason));
    }
  
    static all(promises) {
      // 省略了Promise.all的实现细节，这是一个相对复杂的方法，需要处理多个Promise并确保它们都完成后再返回结果
    }
  
    static race(promises) {
      // 省略了Promise.race的实现细节，这个方法只需要其中一个Promise完成就返回结果
    }
  
    // 辅助函数，用于处理Promise链式调用中的resolve逻辑
    static resolvePromise(promise2, x, resolve, reject) {
      if (promise2 === x) {
        return reject(new TypeError('Chaining cycle detected for promise #<MyPromise>'));
      }
  
      if (x instanceof MyPromise) {
        x.then(
          value => resolvePromise(promise2, value, resolve, reject),
          reason => reject(reason)
        );
      } else if (x !== null && (typeof x === 'object' || typeof x === 'function')) {
        let called = false;
        try {
          const then = x.then;
          if (typeof then === 'function') {
            then.call(
              x,
              y => {
                if (!called) {
                  called = true;
                  resolvePromise(promise2, y, resolve, reject);
                }
              },
              r => {
                if (!called) {
                  called = true;
                  reject(r);
                }
              }
            );
          } else {
            resolve(x);
          }
        } catch (e) {
          if (!called) {
            reject(e);
          }
        }
      } else {
        resolve(x);
      }
    }
  }